<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>姓名：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="姓名"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>手机号：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.mobile"
                        clearable
                        placeholder="手机号"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>机构：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.groupId"
                        filterable
                        clearable
                        placeholder="请选择机构"
                        remote
                        reserve-keyword
                        :remote-method="loadGroup"
                        :loading="flag.groupFlag"
                    >
                        <el-option
                            v-for="item in groups"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <label>关键词：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.keyword"
                        clearable
                        placeholder="姓名|手机号|学号"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                    >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                    >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="warning"
                style="margin-bottom: 2em"
                icon="el-icon-lock"
                :disabled="selectDisable"
                @click="batchLock(false)"
                >批量禁用
            </el-button>
            <el-button
                type="info"
                style="margin-bottom: 2em"
                icon="el-icon-unlock"
                :disabled="selectDisable"
                @click="batchLock(true)"
                >批量启用
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                @selection-change="tableSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="头像"
                    width="80"
                    align="center"
                >
                    <template slot-scope="scope">
                        <el-avatar :src="coverImage(scope.row.header)"
                            >{{ scope.row.name }}
                        </el-avatar>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="昵称（名字）"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="mobile"
                    label="手机号"
                    width="120"
                ></el-table-column>
                <el-table-column
                    prop="schoolName"
                    label="机构"
                    width="120"
                ></el-table-column>
                <el-table-column
                    prop="stuNo"
                    label="学号"
                    width="120"
                ></el-table-column>

                <el-table-column
                    prop="createdTime"
                    label="注册时间"
                    width="180"
                ></el-table-column>
                <el-table-column
                    prop="loginInfo.loginTime"
                    label="最后登录时间"
                    width="160"
                ></el-table-column>
                <el-table-column
                    prop="loginInfo.loginIp"
                    label="最后登录IP"
                    width="120"
                ></el-table-column>
                <el-table-column
                    prop="loginInfo.loginIp"
                    label="最后登录设备"
                    width="120"
                    align="center"
                >
                    <template slot-scope="scope">
                        <k-tag
                            v-if="
                                scope.row.loginInfo &&
                                    scope.row.loginInfo.deviceType
                            "
                            :tag="deviceType[scope.row.loginInfo.deviceType]"
                        ></k-tag>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            :loading="scope.row.opLoading"
                            class="warning"
                            size="mini"
                            type="text"
                            icon="el-icon-refresh-right"
                            @click="resetPass(scope.row)"
                            >重置密码
                        </el-button>
                        <el-button
                            :loading="scope.row.opLoading"
                            size="mini"
                            type="text"
                            icon="el-icon-folder-opened"
                            @click="go('/user/member-detail?id=' + scope.row.id)"
                            >详情
                        </el-button>
                        <el-button
                            v-if="scope.row.userStatus === 'normal'"
                            :loading="scope.row.opLoading"
                            class="warning"
                            size="mini"
                            type="text"
                            icon="el-icon-lock"
                            @click="changeUserStatus(scope.row, false)"
                            >禁用
                        </el-button>
                        <el-button
                            v-if="scope.row.userStatus === 'lock'"
                            :loading="scope.row.opLoading"
                            size="mini"
                            type="text"
                            icon="el-icon-unlock"
                            @click="changeUserStatus(scope.row, true)"
                            >启用
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
    </div>
</template>

<script>
import Fetch from "@/api/request.js";
import { TablePage } from "@/layout/mixin/iVue.js";
import { mapState } from "vuex";
import { get, put, post } from "@/api/request";
import { treeList2Tree } from "@/utils";

export default {
    name: "StudentList",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            queryData: {
                name: "",
                mobile: "",
                groupId: null,
                keyword: null,
                owner: false,
                userType: 16,
                loginInfo: true,
                orders: [
                    {
                        column: "created_time",
                        asc: false
                    }
                ]
            },
            groups: [],
            flag: {
                groupFlag: false,
            }
        };
    },
    computed: {
        ...mapState({
            userChannelType: state => state.status.userChannelType,
            deviceType: state => state.status.deviceType
        })
    },
    watch: {

    },
    created() {
        this.loadGroup();
    },
    mounted() {},
    methods: {
        search(queryData) {
            this.loadTable("/user/cu/member/admin-page", queryData);
        },
        async batchLock(status) {
            if (!status) {
                await this.$confirm("确定禁用这些账号吗？", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                });
            }
            this.tableData
                .filter(v => this.tableSelect.includes(v.id))
                .forEach(v => this.changeUserStatus(v, status));
        },
        changeUserStatus(data, status) {
            data.opLoading = true;
            post(`/user/cu/admin/user/enable/${status}/${data.id}`)
                .then(res => {
                    data.userStatus = status ? "normal" : "lock";
                })
                .finally(() => {
                    data.opLoading = false;
                });
        },
        loadGroup(query) {
            return this.flagPromise(
                get("/user/sys/group/list", {
                    name: query,
                    size: 50,
                }).then(res => {
                    this.groups = res;
                    return res;
                }),
                "groupFlag"
            );
        },
        async resetPass(row) {
            await this.$confirm("确定将密码重置为tk123456吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            });
            post(`/user/cu/user/change/password-admin/${row.id}`, {
                newPassword: "tk123456"
            })
                .then(() => {
                    this.$message.success("重置成功");
                })
                .catch(e => {
                    this.$message.error("重置失败");
                });
        }
    }
};
</script>

<style lang="scss" scoped></style>
